<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Youku
 *
 * @author dragon.hu add @2016.3.10
 */
require_once 'BaseChannel.php';

//优酷
class Youku implements BaseChannel 
{
	static $myurl = "http://defandroidofficialpay.zeronb.com/master/mcGM/payment/Youku/android";
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="56a98d39a1a3e82b20235059bd11232e"; //paykey
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //秘钥
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("youku", $_REQUEST);
		
		$apporderID = $_REQUEST['apporderID'];
		$uid = $_REQUEST['uid'];
		$price = $_REQUEST['price'];
		$sign = $_REQUEST['sign'];
		$passthrough = $_REQUEST['passthrough'];
		$result = $_REQUEST['result'];
		$success_amount = $_REQUEST['success_amount'];
		
		$mystr = self::$myurl.'?'.'apporderID='.$apporderID.'&price='.$price.'&uid='.$uid;
		$mysign = hash_hmac('md5', $mystr, $paymentKey);
		
		if($sign == $mysign)
		{ //验证通过
			//进行发货
			if(false == $orderLog->recordOrder(STATUS_SUCCESS, $price/100, $apporderID, "youku", $platform))
			{
				$result['status'] = 'failed';
				$result['desc'] = urlencode('通知失败');
				$orderLog->log("youku",array("order"=>$apporderID,"money"=>$price,"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$result['status'] = 'success';
				$result['desc'] = urlencode('通知成功');
				$orderLog->log("youku",array("order"=>$apporderID,"money"=>$price,"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			$result['status'] = 'failed';
			$result['desc'] = urlencode('数字签名错误');
			$orderLog->log("youku","  order:$apporderID  sign error sign:$sign mysign:$mysign");
		}
		$ret = json_encode($result);
		return urldecode($ret);
	}
	
}
